using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace System.WebControls
{
	public enum TextTypes
	{
		CurrencyText,
		DecimalText,
		PlainText
	}
	
	/// <summary>
	/// Summary description for TextBoxPlus.
	/// </summary>
	[DefaultProperty("LabelText"), 
		ToolboxData("<{0}:TextBoxPlus runat=server></{0}:TextBoxPlus>")]
	public class TextBoxPlus : System.Web.UI.WebControls.TextBox
	{
		private string _labelText;
		private TextTypes _textType = TextTypes.PlainText;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string LabelText 
		{
			get
			{
				return _labelText;
			}

			set
			{
				_labelText = value;
			}
		}

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue(TextTypes.PlainText)] 
		public TextTypes TextType 
		{
			get
			{
				return _textType;
			}

			set
			{
				_textType = value;
			}
		}

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			output.Write(LabelText);
			if ( Page.IsPostBack ) 
			{
				if ( _textType != TextTypes.PlainText )
				{
					this.FormatText();
				}
			}
			base.Render(output);
		}

		protected void FormatText()
		{
			switch(_textType)
			{
				case TextTypes.CurrencyText:
					this.Text=(double.Parse(this.Text)).ToString("C");
					break;
				case TextTypes.DecimalText:
					this.Text=(Convert.ToInt32(this.Text)).ToString("F");
					break;
			}
		}
	}
}
